function HeatPoint(x,y){
    this.X = x;
    this.Y = y;
    if(HeatPoint.firstClickTime < 0 || HeatPoint.firstClickTime  === undefined){
        HeatPoint.firstClickTime = new Date().getTime();
    }

    this.TimeStamp = (new Date().getTime())- HeatPoint.firstClickTime;

    this.getTime = function(){
        return this.TimeStamp;
    };

    this.asArray = function() {
        return new Array(this.X,this.Y,this.TimeStamp);
    };
}

$(document).ready(function() {
    $('.HeatmapMouseMove').each(function(index,obj){
        var $obj = $(obj);
        obj.HeatPointListMouseMove = new Array();
        $obj.data("Heatmap", h337.create({"element":obj, "radius":25, "visible":true}));
        initHeatmapMouseMoveForObject($obj);
    });

});


function initHeatmapMouseMoveForObject($obj){
    var active = false,
    lastCoords = [],
    mouseMove = false,
    mouseOver = false,
    timer = null,
    obj = $obj[0],
    xx = $obj.data("Heatmap");
     if (synectic.stopanalyse) return;
    activate = function(){
        active = true;
    },
    simulateEv = function(){

        obj.HeatPointListMouseMove.push(new HeatPoint(lastCoords[0], lastCoords[1]));
        //xx.store.addDataPoint(lastCoords[0], lastCoords[1]);
    },

    antiIdle = function(){
        if(mouseOver && !mouseMove && lastCoords && !timer){
            timer = setInterval(simulateEv, 1000);          
        }
    };

    (function(fn){
        setInterval(fn, 1000);
    }(antiIdle));

    obj.onmouseout = function(){
        mouseOver = false;
        if(timer){
            clearInterval(timer)
            timer = null;
        }
    };
    obj.onmousemove = obj.onclick = function(ev){
        mouseMove = true;
        mouseOver = true;
        if(active){
            if(timer){
                clearInterval(timer);
                timer = null;
            }
            var pos = h337.util.mousePosition(ev);
                //xx.store.addDataPoint(pos[0], pos[1]);
                obj.HeatPointListMouseMove.push(new HeatPoint(pos[0], pos[1]));
                lastCoords = [pos[0], pos[1]];
            
            active = false;
        }
        mouseMove = false;
    };

    obj["ontouchmove"] = function(ev){  
        var touch = ev.touches[0],  
        // simulating a mousemove event           
        simulatedEvent = document.createEvent("MouseEvent");  
        simulatedEvent.initMouseEvent(  "mousemove", true, true, window, 1,  
                                        touch.screenX, touch.screenY,  
                                        touch.clientX, touch.clientY, false,  
                                        false, false, false, 0, null);  
        // dispatching the simulated event              
        touch.target.dispatchEvent(simulatedEvent);  
        // we don't want to have the default iphone scrolling behaviour ontouchmove  
        ev.preventDefault();  
    };  

    (function(fn){
        setInterval(fn, 50);
    }(activate));
}

function HeatMapMouseMoveArea(xObj){

    var t = this;

    this.heatPointList = new Array();                         
    this.domObj = xObj;

    this.init = function(heatmap){

        if(heatmap == null) t.heatmap = h337.create({"element":t.domObj, "radius":15, "visible":true});
        else t.heatmap = heatmap;

        //Deaktiviert die Darstellung der Heatmap (display: none)     
        t.heatmap.toggleDisplay();
    };
    this.storeInHeatmap = function(){
        $.each(t.heatPointList, function(index, val) {
            t.heatmap.store.addDataPoint(val.x,val.y);        
        });
    };
    this.positionHeatmap = function(){

        var parent = t.domObj;
        var position = parent.offset();

        var canvas = t.heatmap.get("canvas");
        canvas.offset(position);
        canvas.css("width",parent.outerWidth( true ));
        canvas.css("height",parent.outerHeight( true ));    
    };
    this.domObj.onclick = function(ev){

        if (synectic.stopanalyse) return;

        var pos = h337.util.mousePosition(ev);
        t.heatPointList.push(new HeatPoint(pos[0],pos[1]));
    };
}



